﻿using com.winter.effect;
using com.winter.manager;
using UnityEngine;

namespace com.winter.bird
{
    public class Bird : MonoBehaviour
    {
        public enum bird_STATE
        {
            normal,
            readying,
            fly,
            land,
        }

        [Header("小鸟的状态")] public bird_STATE state = bird_STATE.normal;

        [Header("小鸟的旋转的锚点位置")] public Transform RightTransform;
        [Header("小鸟连接弹弓左边锚点的位置")] public Transform leftTransform;
        [Header("画小鸟到右边树枝的连线的线渲染器")] public LineRenderer rightLineRenderer;
        [Header("画小鸟到左边树枝的连线的线渲染器")] public LineRenderer leftLineRenderer;
        [Header("小鸟离锚点的最大距离")] public float maxDistance = 3;

        /// <summary>
        /// 小鸟挂载的2d弹簧组件
        /// </summary>
        [HideInInspector] public SpringJoint2D springJoint2D;

        /// <summary>
        /// 小鸟挂载的2d刚体组件
        /// </summary>
        private new Rigidbody2D rigidbody2D;

        /// <summary>
        /// 小鸟消失时的云朵效果
        /// </summary>
        public GameObject boom;

        /// <summary>
        /// 主相机
        /// </summary>
        private Camera mainCamera;

        /// <summary>
        /// 右边树枝的位置
        /// </summary>
        private Vector3 rightWoodPos;

        public TestMyTrail trail;

        private void Awake()
        {
            rightWoodPos = RightTransform.position;

            mainCamera = Camera.main;

            springJoint2D = GetComponent<SpringJoint2D>();

            rigidbody2D = GetComponent<Rigidbody2D>();

            trail = GetComponent<TestMyTrail>();
        }

        private void OnMouseDown()
        {
            if (state == bird_STATE.normal && GameManager.getInstance().gameState == GameManager.game_STATE.running)
            {
                state = bird_STATE.readying;
                rigidbody2D.isKinematic = true;
            }
        }

        private void OnMouseUp()
        {
            if (state == bird_STATE.readying)
            {
                state = bird_STATE.fly;
                rigidbody2D.isKinematic = false;
                Invoke(nameof(fly), 0.1f);

                // 禁用画线组件
                rightLineRenderer.enabled = false;
                leftLineRenderer.enabled = false;
            }
        }

        private void Update()
        {
            if (state == bird_STATE.readying)
            {
                // 控制小鸟跟随鼠标
                Vector3 position = mainCamera.ScreenToWorldPoint(Input.mousePosition);
                position -= new Vector3(0, 0, mainCamera.transform.position.z);

                transform.position = position;

                // 限制小鸟能被拉长的最大长度
                if (Vector3.Distance(transform.position, rightWoodPos) > maxDistance)
                {
                    // 计算小鸟离锚点的方向向量
                    Vector3 normalized = (transform.position - rightWoodPos).normalized;

                    // 计算小鸟离锚点的向量
                    normalized *= maxDistance;

                    // 设置小鸟位置向量
                    transform.position = normalized + rightWoodPos;
                }

                drawLineFromBirdToWood();
            }
        }

        /// <summary>
        /// 控制小鸟飞行时不在受SpringJoint2D
        /// 弹簧控件的物理影响， 让小鸟的速度不至于太大
        /// </summary>
        private void fly()
        {
            springJoint2D.enabled = false;
            trail.trailStart();
            Invoke(nameof(turnToNextBird), 5);
        }


        /// <summary>
        /// 画小鸟到弹弓两条树枝的线
        /// </summary>
        private void drawLineFromBirdToWood()
        {
            // 禁用画线组件
            rightLineRenderer.enabled = true;
            leftLineRenderer.enabled = true;

            rightLineRenderer.SetPosition(0, rightWoodPos);
            rightLineRenderer.SetPosition(1, transform.position);

            leftLineRenderer.SetPosition(0, leftTransform.position);
            leftLineRenderer.SetPosition(1, transform.position);
        }

        /// <summary>
        /// 轮换到下一只的小鸟方法
        /// </summary>
        private void turnToNextBird()
        {
            GameManager.getInstance().birds.Remove(this);
            Destroy(gameObject);
            Instantiate(boom, transform.position, Quaternion.identity);
            GameManager.getInstance().checkGameState();
        }

        // ReSharper disable once UnusedParameter.Local
        private void OnCollisionEnter2D(Collision2D other)
        {
            trail.weaponTrail.ClearTrail();
        }
    }
}